﻿using Microsoft.Extensions.DependencyInjection;
using Sven.Core.Libs;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;

namespace Sven.Core.Extensions
{
    /// <summary>
    /// IServiceCollection 扩展
    /// </summary>
    public static class ServiceExtension
    {
        /// <summary>
        /// IServiceCollection 扩展
        /// IUserService --> UserService, IUserRepository --> UserRepository.
        /// </summary>
        /// <param name="service"></param>
        /// <param name="assemblyName">程序集的全称不含扩展名</param>
        /// <returns></returns>
        //public static IServiceCollection RegisterAssembly(this IServiceCollection service, string assemblyName)
        //{
        //    if (service == null)
        //        throw new ArgumentNullException(nameof(service));

        //    if (String.IsNullOrEmpty(assemblyName))
        //        throw new ArgumentNullException(nameof(assemblyName));

        //    var assembly = RuntimeHelper.GetAssemblyByFullName(assemblyName);
        //    if (assembly == null)
        //        throw new DllNotFoundException($"\"{assemblyName}\".dll不存在");

        //    //获取接口，过滤泛型接口
        //    var interfacesList = assembly.GetTypes().Where(t => t.GetTypeInfo().IsInterface && !t.GetTypeInfo().IsGenericType).ToList();
        //    foreach (var type in interfacesList)
        //    {
        //        //实现的名称
        //        var implementTypeName = type.Name.Substring(1);
        //        //查找当前程序集
        //        var implementType = RuntimeHelper.GetImplementType(implementTypeName, type, assembly);
        //        //如果当前程序集不存在查找所有程序集
        //        if (implementType == null)
        //            implementType = RuntimeHelper.GetImplementType(implementTypeName, type);

        //        if (implementType != null)
        //            service.AddSingleton(type, implementType);
        //    }
        //    return service;
        //}

        public static IServiceCollection RegisterAssembly(this IServiceCollection service, string assemblyName, EnumInjection injection)
        {
            if (service == null)
                throw new ArgumentNullException(nameof(service));

            if (String.IsNullOrEmpty(assemblyName))
                throw new ArgumentNullException(nameof(assemblyName));

            var assembly = RuntimeHelper.GetAssemblyByFullName(assemblyName);
            if (assembly == null)
                throw new DllNotFoundException($"\"{assemblyName}\".dll不存在");

            //获取类型
            var typeList = assembly.GetTypes().Where(t => t.GetTypeInfo().IsClass && !t.GetTypeInfo().IsAbstract && !t.GetTypeInfo().IsGenericType).ToList();
            foreach (var type in typeList)
            {
               var faces = type.GetInterfaces();
                if (faces.Any())
                {
                   var interfaceType = faces.FirstOrDefault();
                    switch (injection)
                    {
                        case EnumInjection.Scoped:
                            service.AddScoped(interfaceType, type);
                            break;
                        case EnumInjection.Singleton:
                            service.AddSingleton(interfaceType, type);
                            break;
                        case EnumInjection.Transient:
                            service.AddTransient(interfaceType, type);
                            break;
                    }
                }
            }
            return service;
        }
    }

    /// <summary>
    /// 注册方式
    /// </summary>
    public enum EnumInjection
    {
        /// <summary>
        /// 
        /// </summary>
        Scoped,

        /// <summary>
        /// 
        /// </summary>
        Singleton,

        /// <summary>
        /// 
        /// </summary>
        Transient
    }
}
